const Controller = require('egg').Controller;
//删除用户id
const Delete = {
  id: { type: 'string', required: true },
};
//创建用户
const createdUser = {
  phone: { type: 'number', required: true },
  userName: { type: 'string', required: true },
  passWord: { type: 'string', required: true },
};

class users extends Controller {
  async index() {
    const { ctx, service } = this;
    const data = await service.user.users.index(ctx.request.query);
    return ctx.helper.successTotal(ctx, 'success', data.result, data.total);
  }
  async create() {
    const { ctx, service } = this;
    ctx.validate(createdUser, ctx.request.body);
    const data = await service.user.users.create(ctx.request.body);
    if (data === 0) {
      return ctx.helper.success(ctx, '创建成功', data);
    } else if (data === 1) {
      return ctx.helper.error(ctx, '创建失败', data);
    } else if (data === 2) {
      return ctx.helper.error(ctx, '记录已经存在,清重新输入', data);
    }
  }
  async update() {
    const { ctx, service } = this;
    ctx.validate(createdUser, ctx.request.body);
    const data = await service.user.users.update(ctx.request.body, ctx.params);
    if (data) {
      return ctx.helper.error(ctx, '修改失败', data);
    } else {
      return ctx.helper.success(ctx, '修改成功', data);
    }
  }
  async destroy() {
    const { ctx, service } = this;
    const data = await service.user.users.destroy(ctx.params);
    if (data === 1) {
      return ctx.helper.error(ctx, '删除失败', { msg: '参数错误' });
    } else {
      return ctx.helper.success(ctx, '删除成功', data);
    }
  }
}

module.exports = users;
